package fr.mistra.exercice.bibliotheque.presentation;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.mistra.exercice.bibliotheque.dao.DAOException;
import fr.mistra.exercice.bibliotheque.dao.NoSuchIdException;
import fr.mistra.exercice.bibliotheque.modeles.Livre;
import fr.mistra.exercice.bibliotheque.services.LivreService;

/**
 * Servlet implementation class VoirLivreServlet
 */
public class VoirLivreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VoirLivreServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		long id = Long.valueOf(request.getParameter("id")).longValue();
		try {
			LivreService service = LivreService.getLivreService();
			Livre l = service.getLivre(id);
			request.setAttribute("livre", l);
			System.out.println("Go livre !!!");
			request.getRequestDispatcher("/JSP/VoirLivre.jsp").forward(request, response);
		}
		catch (DAOException e) {
			e.printStackTrace();
			request.getRequestDispatcher("/JSP/ErreurDAO.jsp").forward(request, response);
		}
		catch (NoSuchIdException e) {
			e.printStackTrace();
			System.out.println("Impossible d'afficher la liste des livres de la bibliotheque n�" + id);
			request.getRequestDispatcher("/JSP/ErreurDAO.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//Aucune raison d'arriver l� via une m�thode POST
		request.getRequestDispatcher("/curieux.jsp").forward(request, response);
	}
}
